package cn.fhj.sohu;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.fhj.util.DateUtil;
import cn.fhj.util.HashUtil;

public class Comment {
	private Comment rt;
	private String time;

	private boolean saved;

	private List<String> pics;

	private long id;

	private String pic;

	private String author;

	public String getPic() {
		return pic;
	}

	public void setPic(String pic) {
		this.pic = pic;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	private boolean multiPic;

	public boolean isMultiPic() {
		return multiPic;
	}

	public void setMultiPic(boolean multiPic) {
		this.multiPic = multiPic;
	}

	public boolean equals(Object o) {
		if (!(o instanceof Comment)) {
			return false;
		}
		return id == ((Comment) o).id;
	}

	public int hashCode() {
		return HashUtil.calHash(id);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Comment getRt() {
		return rt;
	}

	public void setRt(Comment rt) {
		this.rt = rt;
	}

	public String getTime() {
		return time;
	}

	public boolean isSaved() {
		return saved;
	}

	public void setSaved(boolean saved) {
		this.saved = saved;
	}

	public void setTime(String time) {
		// 4秒前
		// 57分钟前
		// 1小时前
		// 今天17:28
		// 昨天23:16
		// 前天23:22
		time = time.replaceAll("\\s+", "").replaceAll(String.valueOf((char) 160), "");
		Date date = new Date();
		Date result = null;
		int dayIndex = time.indexOf('天');
		if (dayIndex > 0) {
			String tmpTime = time.substring(dayIndex + 1);
			if (time.contains("今天")) {
				time = DateUtil.format(date, "MM月dd日");
			} else if (time.contains("昨天")) {
				time = DateUtil.format(DateUtil.addDays(date, -1), "MM月dd日");
			} else {
				time = DateUtil.format(DateUtil.addDays(date, -2), "MM月dd日");
			}
			time = time + ' ' + tmpTime;
		} else if (time.contains("前")) {
			int count = -Integer.valueOf(time.replaceAll("\\D+", ""));
			if (time.contains("秒前")) {
				result = DateUtil.addSeconds(date, count);
			} else if (time.contains("分钟前")) {
				result = DateUtil.addMinutes(date, count);
			} else {
				result = DateUtil.addHours(date, count);
				result = DateUtil.addMinutes(date, -30);
			}
		}
		this.time = result != null ? DateUtil.format(result, time.contains("小时前") ? "MM-dd HH:mm?" : "MM-dd HH:mm")
				: time;
		this.time = this.time.replace('月', '-');
		this.time = this.time.replace('日', ' ');
		if (this.time.length() < 11) {
			this.time = this.time.substring(0, 5) + ' ' + this.time.substring(5);
		}
	}

	public boolean isVagueTime() {
		return getTime().indexOf('?') > 0;
	}

	public List<String> getPics() {
		return pics;
	}

	public void addPic(String pic) {
		if (pics == null) {
			pics = new ArrayList();
		}
		pics.add(pic);
	}

	public String getPicStatu() {
		if (isMultiPic()) {
			return "2";
		} else if (getPic() != null) {
			return getPic();
		} else {
			return "0";
		}
	}

	public void setPicStatu(String status) {
		if (status.equals("2")) {
			setMultiPic(true);
		} else if (!status.equals("0")) {
			setPic(status);
		}
	}

	public String getJsFile() {
		return getDir() + "/" + id + ".js";
	}
	public String getDir() {
		return "comment/" + id % 100;
	}
}
